<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户名称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="广告等级" prop="advertLevel">
        <el-select
          v-model="queryParams.advertLevel"
          placeholder="请选择广告等级"
          clearable
        >
          <el-option
            v-for="dict in levelListData"
            :key="dict.levelId"
            :label="dict.levelName"
            :value="dict.levelId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.user_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <div>
      <p style="color: #606266; font-size: 14px; font-weight: 700">
        用户总余额：{{ count || 0 }}元
      </p>
    </div>

    <el-table class="box1" v-loading="loading" :data="orderList">
      <el-table-column
        label="用户ID"
        align="center"
        prop="userId"
        width="180"
      />
      <el-table-column
        label="用户名称"
        align="center"
        prop="nickname"
        width="150"
      />
      <el-table-column
        label="手机号码"
        align="center"
        prop="mobile"
        width="150"
      />
      <el-table-column
        label="账户余额"
        align="center"
        prop="balance"
        width="150"
      />
      <el-table-column label="邀请码" align="center" prop="salt" />
      <el-table-column
        label="推荐人"
        align="center"
        prop="parentId"
        width="200"
      >
        <template slot-scope="scope">
          <p>昵称:{{ scope.row.proUser.nickname }}</p>
          <p>手机号:{{ scope.row.proUser.mobile }}</p>
          <p>ID:{{ scope.row.proUser.id }}</p>
        </template>
      </el-table-column>
      <el-table-column label="广告收益等级" align="center" prop="advertLevel">
        <template slot-scope="scope">
          <div v-if="scope.row.advertLevel">
            {{ scope.row.advertLevel | getLabel(levelListData) }}
          </div>
          <div v-else>无</div>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" align="center">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.user_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="note"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.status == 2"
              @click="userDeal(scope.row, 1)"
              >启用</el-button
            >
          </div>
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.status == 1"
              @click="userDeal(scope.row, 2)"
              >禁用</el-button
            >
          </div>
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="!scope.row.lockBalance"
              @click="moneyOprat(scope.row, true)"
              >锁定余额</el-button
            >
          </div>
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.lockBalance"
              @click="moneyOprat(scope.row, false)"
              >解冻余额</el-button
            >
          </div>
          <div>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="userMoney(scope.row, 2)"
              >操作余额</el-button
            >
          </div>
          <div>
            <el-button
              style="margin-right: 14px; margin-left: 0"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="changeUserLevelFunc(scope.row)"
              >修改广告等级</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改用户余额 -->
    <el-dialog
      :title="title"
      :visible.sync="openMoney"
      width="500px"
      append-to-body
      @close="closeFunc"
    >
      <el-form
        ref="formMoney"
        :model="formMoney"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="操作类型" prop="add">
          <el-radio-group ref="radio" v-model="formMoney.add">
            <el-radio :label="true">充值</el-radio>
            <el-radio :label="false">划扣</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="金额" prop="balance">
          <el-input
            v-model="formMoney.balance"
            type="number"
            placeholder="请输入金额"
          />
        </el-form-item>
        <el-form-item label="备注" prop="rema">
          <el-input
            v-model="formMoney.rema"
            type="textarea"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改用户广告等级 -->
    <el-dialog
      title="修改用户广告等级"
      :visible.sync="openLevel"
      width="500px"
      append-to-body
      @close="closeFunc"
    >
      <el-form
        ref="formLevelInfo"
        :model="formLevelInfo"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="用户名称" prop="nickname">
          <el-input
            disabled
            v-model="formLevelInfo.nickname"
            type="text"
            placeholder="请输入用户名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="广告等级" prop="advertLevel">
          <el-select
            v-model="formLevelInfo.advertLevel"
            placeholder="请选择广告等级"
            clearable
          >
            <el-option
              v-for="dict in levelListData"
              :key="dict.levelId"
              :label="dict.levelName"
              :value="dict.levelId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormLevel">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  customerList,
  changeUser,
  changeBalance,
  userLevelList,
  changeUserCount,
  changeUserLevel,
} from "@/api/customer/customer";
export default {
  name: "customerList",
  dicts: ["user_level_list", "user_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "操作余额",
      // 是否显示弹出层
      open: false,
      openLevel: false, //调整等级探出窗
      openRelation: false, //调整关系链
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        userId: "",
        mobile: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        advertLevel: [
          { required: true, message: "请选择广告等级", trigger: "change" },
        ],
        walletId: [
          { required: true, message: "请选择账户类型", trigger: "change" },
        ],
        income: [
          { required: true, message: "请选择调整类型", trigger: "change" },
        ],
        amount: [{ required: true, message: "请输入金额", trigger: "blur" }],
        memo: [{ required: true, message: "请输入备注", trigger: "blur" }],
        billLevel: [
          { required: true, message: "请选择报单等级", trigger: "change" },
        ],
        userLevel: [
          { required: true, message: "请选择消费等级", trigger: "change" },
        ],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        parentMobile: [
          { required: true, message: "请输入上级手机号", trigger: "blur" },
        ],

        add: [{ required: true, message: "请选择操作类型", trigger: "change" }],
        balance: [
          { required: true, message: "请输入操作金额", trigger: "blur" },
        ],
        rema: [{ required: true, message: "请输入操作备注", trigger: "blur" }],
      },
      openUp: false,
      openDown: false,
      upDataList: [],
      downDataList: [],
      totalUp: 0,
      totalDown: 0,
      userId: null,
      levelRemark: null, //调整等级备注
      userInfo: {},
      formMoney: {
        userId: null,
        add: null,
        balance: null,
        rema: null,
      },
      openMoney: false,
      levelListData: [],
      formLevelInfo: {
        advertLevel: null,
        id: null,
        nickname: null,
      },
      count: 0,
    };
  },
  filters: {
    getLabel(e, arr) {
      let label = "";
      let arra = arr.filter((item) => e == item.levelId);
      if (arra.length) {
        label = arra[0].levelName;
      }
      return label;
    },
  },
  created() {
    this.getLevelList();
    this.getList();
  },
  methods: {
    getLevelList() {
      // levelId: 1, levelName: "广告普通会员",
      userLevelList().then((res) => {
        if (res.code == 200) {
          this.levelListData = res.data.records;
        }
      });
    },
    closeFunc() {
      this.openLevel = false;
      this.openMoney = false;
    },
    // 修改用户等级
    changeUserLevelFunc(data) {
      this.formLevelInfo = {
        advertLevel: data.advertLevel ? Number(data.advertLevel) : null,
        nickname: data.nickname,
        id: data.id,
      };
      this.openLevel = true;
    },
    // 操作余额
    userMoney(data) {
      this.userInfo = data;
      this.formMoney = {
        userId: data.userId,
        add: null,
        balance: null,
        rema: null,
      };
      this.openMoney = true;
    },
    // 修改用户余额
    submitForm() {
      let that = this;
      that.$refs["formMoney"].validate((valid) => {
        if (valid) {
          changeBalance(that.formMoney).then((res) => {
            if (res.code == 200) {
              that.$modal.msgSuccess("操作成功");
              that.openMoney = false;
              that.formMoney = {
                userId: null,
                add: null,
                balance: null,
                rema: null,
              };
              that.getList();
            }
          });
        }
      });
    },
    // 修改用户等级
    submitFormLevel() {
      let that = this;
      that.$refs["formLevelInfo"].validate((valid) => {
        if (valid) {
          changeUserLevel(that.formLevelInfo).then((res) => {
            if (res.code == 200) {
              that.$modal.msgSuccess("修改用户广告等级成功");
              that.openLevel = false;
              that.formLevelInfo = {
                advertLevel: null,
                id: null,
                nickname: null,
              };
              that.getList();
            }
          });
        }
      });
    },
    cancel() {
      this.openMoney = false;
      this.formMoney = {
        userId: null,
        add: null,
        balance: null,
        rema: null,
      };
      this.openLevel = false;
      this.formLevelInfo = {
        advertLevel: null,
        id: null,
        nickname: null,
      };
    },
    // 用户操作
    userDeal(row, type) {
      // 弹出窗确认
      this.$modal.confirm("确认执行操作吗？").then(() => {
        this.userChangeAjax(row, type);
      });
    },
    // 用户操作
    moneyOprat(row, type) {
      // 弹出窗确认
      let that = this;
      let name = type ? "锁定余额" : "解冻余额";
      that.$modal.confirm("确认执行" + name + "操作吗？").then(() => {
        let data = {
          id: row.id,
          lockBalance: type,
        };
        changeUserCount(data).then((res) => {
          if (res.code == 200) {
            that.$modal.msgSuccess(name + "成功");
            that.getList();
          }
        });
      });
    },
    userChangeAjax(row, type) {
      let data = {
        id: row.id,
        status: type,
      };
      changeUser(data).then((res) => {
        if (res.code == 200) {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        }
      });
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      if (this.dateRange && this.dateRange.length > 0) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
      customerList(this.queryParams).then((response) => {
        this.orderList = response.data.records;
        this.total = response.data.total;
        this.count = response.count;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.beginTime = "";
      this.queryParams.endTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="css">
.el-tooltip__popper {
  max-width: 40%;
}
</style>
